
/**
 * 
 * \brief This class validates the user reigisters users to the system
 * \author Salman Noor
 */

$(function(){




    var firstName = jQuery(':input[id=firstName]');
    var lastName = jQuery(':input[id=lastName]');
    var userName = jQuery(':input[id=userName]');
    var formEmail = jQuery(':input[id=emailAddress]');
    var formPassword = jQuery(':input[id=passwordField]');
    var formRepeatPassword = jQuery(':input[id=repeatPasswordField]');
    var nickName = jQuery(':input[id=nickName]');
    var commentatorProfile = jQuery(':input[id=commentatorProfile]');
    $("#profileInputContainer").hide();


   // $('#container_userType').buttonset();
    //jQuery('.ui-button-text').css('width','250px');

    var hiddenResponseDiv = jQuery('.responseDiv');
    hiddenResponseDiv.hide();
    var allFields = jQuery([]).add(firstName).add(lastName).add(userName).add(formEmail).add(formPassword).add(formRepeatPassword).add(commentatorProfile).add(nickName);

    $(".registerButton").button();

    $(".registerButton").button({

        icons: {
            primary: 'ui-icon-comment'
        },
        text: true
    });

    $("#dialog-box-registration").dialog({
        resizable: true,
        width: 540,
        modal: true,
        show: 'slide',
      //  hide: 'clip',
        autoOpen: false,
        closeOnEscape: false,
close: function(event, ui) {
allFields.val("");
parent.location.hash = '';
}
    });

    $("input:radio[name=userType]").change(function(){
    if ($("input:radio[name=userType]:checked").val() == 'commentator')
    {
        $("#profileInputContainer").show('clip','',250);
        $("#nickNameInputContainer").hide('clip','',250);
        $("#nickName").val(null);
    }
    else
    {
        $("#nickNameInputContainer").show("clip",'',500);
        $("#profileInputContainer").hide('clip','',500);
        $("#commentatorProfile").val('');
    }
    });
    

//    $(".registerButton").click(function(){

//        jQuery( "#dialog-box-registration").dialog({
//            title: 'Register as a New User'
//        });

        $("#dialog-box-registration").dialog( "option", "buttons", {
            "Register": function() {
                // jQuery(this).dialog("close");
                var bValid = true;
                allFields.removeClass('ui-state-error');

                bValid = bValid && formValidator.checkLength(firstName,"your first name",2,20);
                bValid = bValid && formValidator.checkRegexp(firstName,/^([a-zA-Z0-9 _-]+)$/,"The first name field only allows alphanumeric characters (a-z 0-9).");
                bValid = bValid && formValidator.checkLength(lastName,"your last name",2,20);
                bValid = bValid && formValidator.checkRegexp(lastName,/^([0-9a-zA-Z])+$/,"The last name field only allows alphanumeric characters (a-z 0-9).");
                if ($("input:radio[name=userType]:checked").val() == 'commentator')
                {
                    bValid = bValid && formValidator.checkLength(commentatorProfile,"your commentator profile",2,100);
                    //bValid = bValid && formValidator.checkRegexp(commentatorProfile,/^([0-9a-zA-Z])+$/,"The commentator profile field only allows alphanumeric characters (a-z 0-9).");
                }
                else
                {
                    bValid = bValid && formValidator.checkLength(nickName,"your nickname",2,20);
                    bValid = bValid && formValidator.checkRegexp(nickName,/^([0-9a-zA-Z])+$/,"The nick name field only allows alphanumeric characters (a-z 0-9).");
                }
                bValid = bValid && formValidator.checkRegexp(formEmail,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"Enter a valid email address, eg. yourname@wits.ac.za.");
                bValid = bValid && formValidator.checkLength(userName,"your user name",1,20);
                bValid = bValid && formValidator.checkRegexp(userName,/^([0-9a-zA-Z])+$/,"The user name field only allows alphanumeric characters (a-z 0-9).");
                bValid = bValid && formValidator.checkLength(formPassword,"your password",1,20);
                bValid = bValid && formValidator.checkRegexp(formPassword,/^([0-9a-zA-Z])+$/,"The password field only allows alphanumeric characters (a-z 0-9).");
                bValid = bValid && formValidator.checkLength(formRepeatPassword,"your password",1,20);
                bValid = bValid && formValidator.checkRegexp(formRepeatPassword,/^([0-9a-zA-Z])+$/,"The password field only allows alphanumeric characters (a-z 0-9).");
                bValid = bValid && formValidator.checkPasswordFields(formPassword,formRepeatPassword);


                if (bValid) {




                    sendRequest.setAction('addNewUser');

                    var userType1 = $("input:radio[name=userType]:checked").val();
                if ($("input:radio[name=userType]:checked").val() == 'commentator')
                {
                    formData = {
                        'firstName':firstName.val(),
                        'lastName':lastName.val(),
                        'extraUserInfo':commentatorProfile.val(),
                        'formEmail':formEmail.val(),
                        'userName':userName.val(),
                        'formPassword':formPassword.val(),
                        'userType': userType1
                    }
                }
                else
                {
                    formData = {
                        'firstName':firstName.val(),
                        'lastName':lastName.val(),
                        'extraUserInfo':nickName.val(),
                        'formEmail':formEmail.val(),
                        'userName':userName.val(),
                        'formPassword':formPassword.val(),
                        'userType': userType1
                    }
                }
                    sendRequest.setSendParameters(formData);
                    //sendRequest.sentInfo();
                    hiddenResponseDiv.load(sendRequest.serverURL, sendRequest.getSendData(), function(){
                        hiddenResponseDiv.unbind();
                        var registrationResponse = (hiddenResponseDiv.html()).replace(/^\s*|\s*$/g,'');
                        if (registrationResponse == '1')
                        {
                            jQuery("#dialog-box-registration").dialog('close');
                            jQuery('.errorMessageDiv').html('').hide();
                            jQuery('input[id=firstName]').val('');
                            jQuery('input[id=lastName]').val('');
                            jQuery('input[id=userName]').val('');
                            commentatorProfile.val("");
                            nickName.val("");
                            jQuery('input[id=emailAddress]').val('');
                            jQuery('input[id=passwordField]').val('');
                            jQuery('input[id=repeatPasswordField]').val('');
                            jQuery("#dialogue-registrationOKMessage").dialog('open');
                        }
                        else
                        {
                            userName.addClass('ui-state-error');
                            formValidator.updateErrorMessage("User Name Already Exists. Please select another User Name");
                        }
                    });
                }
            },
            "Cancel": function() {
                jQuery(this).dialog('close');
                jQuery('.errorMessageDiv').html('');
                jQuery('input[id=firstName]').val('');
                jQuery('input[id=lastName]').val('');
                jQuery('input[id=userName]').val('');
                jQuery('input[id=emailAddress]').val('');
                jQuery('input[id=passwordField]').val('');
                jQuery('input[id=repeatPasswordField]').val('');
            }
        //jQuery(this).dialog("close");
        });

//    });


    jQuery("#dialogue-registrationOKMessage").dialog({
        resizable: true,
        width:400,
        autoOpen: false,
        modal: true,
        hide: 'puff',
        show: 'puff',
        buttons: {
            'OK': function() {
                jQuery(this).dialog('close');


            }
        }
    });


});